var createError = require('http-errors');
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');

var applist = require('./routes/applist');
var appdetails = require('./routes/appdetails');
var sarelation = require('./routes/server_app_relation');

var methodOverride = require('method-override');
var session = require('express-session');

var http = require('http');
var path = require('path');
var engine = require('ejs-locals');
var url  = require('./utils/url.js');
var mysql = require('mysql');
var flash = require('connect-flash'); 
var groupHandlers = require('express-group-handlers');

//groupHandlers.setup(app);

var cookieParser = require('cookie-parser');
var logger = require('morgan');



var app = express();
groupHandlers.setup(app);

function createSID() {
    var pre = "SESS";
    var time = (new Date()).getTime() + '';
    var id = pre + '_' + (time).substring(time.length - 6) + '_' + (Math.round(Math.random() * 1000));
    return id;
}

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('html',engine);
app.set('view engine', 'html');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(methodOverride());
app.use(flash());
app.use(express.static(path.join(__dirname, 'public')));
app.use(
    session({ 
        secret: 'keyboard cat', 
        key: createSID(), 
        cookie: { secure: true}, 
        cookie: { maxAge: 60 * 60 * 1000}
    })
);

app.use('/', routes.index);
app.use('/logout',routes.logout);
app.use('/login',user.login);
app.use('/validateUser',user.validateUser);
app.use('/toRegister',routes.toRegister);
app.use('/executeRegister',user.executeRegister);

app.beforeEach(function (req, res, next) {
    if (req.session.user) {
        console.log(req.session)
        next();
    } else {
        console.log('错误')
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write("<script>parent.location.href='/'</script>");
        res.end();
    }
}, function (app) {
    app.get('/toMainPage',routes.toMainPage);
    app.get('/getServerList',routes.getServerList);

    app.post('/createServer',routes.createServer);
    app.post('/editServer',routes.editServer);
    app.get('/deleteServer',routes.deleteServer);

    app.get('/getAllApp',applist.getAllApp);
    app.post('/createAppInfo',applist.createAppInfo);
    app.post('/editAppInfo',applist.editAppInfo);
    app.get('/deleteApp',applist.deleteApp);

    app.get('/getAppDetails',appdetails.getAppDetails);
    app.post('/createAppDetails',appdetails.createAppDetails);
    app.post('/editAppDetails',appdetails.editAppDetails);
    app.get('/deleteAppDetails',appdetails.deleteAppDetails);

    app.get('/toAppList' ,sarelation.toAppList);
    app.get('/getAppName' ,sarelation.getAppName);
    app.post('/addServer_App',sarelation.addServer_App);
    app.post('/hasAppId',sarelation.hasAppId);
    app.get('/deleteS_app',sarelation.deleteS_app);

    app.get('/toPlatform',sarelation.toPlatform);
    app.get('/getPlatformName',sarelation.getPlatformName);
    app.post('/addPlatformName',sarelation.addPlatformName);
    app.post('/hasPlatformId',sarelation.hasPlatformId);
    app.get('/deletePlatform',sarelation.deletePlatform);
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});



console.log('服务器运行中')
module.exports = app;
